package com.atguigu.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author lenovo
 * @version 1.0
 * @date 2020/12/28 17:07
 */
@Slf4j
@RestController
public class FlowLimitController {

    int questCount = 0;
    int errorCount = 0;

    @GetMapping("/testA")
    public String testA() throws InterruptedException {

        String name = Thread.currentThread().getName();
        TimeUnit.MILLISECONDS.sleep(800);
        return name + "------------testA-----------";
    }


    @GetMapping("/testB")
    public String testB() throws InterruptedException {

        String name = Thread.currentThread().getName();
//        TimeUnit.MILLISECONDS.sleep(800);
        log.info(name + " testB...start!");
        return name + "------------testB-----------";
    }


    @GetMapping("/testD")
    public String testD() throws InterruptedException {

        String name = Thread.currentThread().getName();

        // RT 测试RT
//        TimeUnit.SECONDS.sleep(1);

        // 异常 测试异常
        int i = new Random().nextInt(3);
        questCount++;
        errorCount = errorCount + (i == 0 ? 1 : 0);
        log.info(name + " testD...随机数【{}】,共请求【{}】次，异常【{}】次！！！", i);
        int a = 10 / i;

        log.info(name + " testD...start!");
        return name + "------------testD-----------";
    }

    @GetMapping("/testE")
    public String testE() throws InterruptedException {

        String name = Thread.currentThread().getName();

        // 异常 测试异常数
        int a = 10 / 0;

        log.info(name + " testE...start!");
        return name + "------------testE-----------";
    }


    @GetMapping("/testHotKey")
    @SentinelResource(value = "testHotKeyResource")
    public String testHotKey(@RequestParam(value = "p1", required = false) String p1,
                             @RequestParam(value = "p2", required = false) String p2) {
        String name = Thread.currentThread().getName();
        log.info(name + " testHotKey....p1={},p2={}!", p1, p2);
        return name + "------------testHotKey------...p1=" + p1 + ",p2=" + p2 + "!--";
    }


    public String deal_testHotKey(String p1, String p2, BlockException e) {
        return "------------deal_testHotKey【/(ㄒoㄒ)/~~】------...p1=" + p1 + ",p2=" + p2 + "!--";
    }


    /**
     * @param content
     * @param key
     * @return
     */
    public static String decryptAES(String content, String key) {
        if (content.length() < 1)
            return null;
        byte[] byteRresult = new byte[content.length() / 2];
        for (int i = 0; i < content.length() / 2; i++) {
            int high = Integer.parseInt(content.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(content.substring(i * 2 + 1, i * 2 + 2), 16);
            byteRresult[i] = (byte) (high * 16 + low);
        }
        try {
            KeyGenerator kgen = KeyGenerator.getInstance("AES");
            kgen.init(128, new SecureRandom(key.getBytes()));
            SecretKey secretKey = kgen.generateKey();
            byte[] enCodeFormat = secretKey.getEncoded();
            SecretKeySpec secretKeySpec = new SecretKeySpec(enCodeFormat, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
            byte[] result = cipher.doFinal(byteRresult);
            return new String(result);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }


//    content：8C10E80FCFD72141CB515AFF0C7CC188
//key: b5e8ccbc2c0c7dacec9bafdefb82f208
//解密结果: 13829119060

    public static void main(String[] args) {
        System.out.println(decryptAES("8C10E80FCFD72141CB515AFF0C7CC188", "b5e8ccbc2c0c7dacec9bafdefb82f208"));
    }


}
